package cn.tangsihai.pet.service.impl;

import cn.tangsihai.basic.exception.MyException;
import cn.tangsihai.basic.service.impl.BasicServiceImpl;
import cn.tangsihai.basic.util.DistanceUtil;
import cn.tangsihai.basic.util.PageList;
import cn.tangsihai.basic.util.Point;
import cn.tangsihai.org.domain.Employee;
import cn.tangsihai.org.domain.Shop;
import cn.tangsihai.org.mapper.EmployeeMapper;
import cn.tangsihai.org.mapper.ShopMapper;
import cn.tangsihai.pet.domain.Pet;
import cn.tangsihai.pet.domain.PetDetail;
import cn.tangsihai.pet.domain.SearchMasterMsg;
import cn.tangsihai.pet.mapper.PetDetailMapper;
import cn.tangsihai.pet.mapper.PetMapper;
import cn.tangsihai.pet.mapper.SearchMasterMsgMapper;
import cn.tangsihai.pet.query.SearchMasterMsgQuery;
import cn.tangsihai.pet.service.ISearchMasterMsgService;
import cn.tangsihai.user.domain.User;
import cn.tangsihai.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class SearchMasterMsgServiceImpl extends BasicServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;


    /**
     * 发布寻主：
     *    流程：分析数据库中需要的字段，将前端提交的数据补存完整存入数据库中即可
     * @param smm
     * @param logininfoId
     */
    @Override
    @Transactional
    public void publish(SearchMasterMsg smm, Long logininfoId) {
        // 获取登录用户id
        User user = userMapper.findByLogininfoId(logininfoId);
        smm.setUser(user);
        smm.setUser_id(user.getId());
        // 获取shop_id
        // 获取当前用户地址
        Point point = DistanceUtil.getPoint(smm.getAddress());
        // 查询所有店铺地址
        List<Shop> shops = shopMapper.loadAll();
        // 获取最近的店铺
        Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
        // 将数据存入数据库中
        smm.setShop(nearestShop);
        smm.setState(0); // 状态：0未处理，1已处理
        smm.setShop_id(nearestShop.getId());
        searchMasterMsgMapper.add(smm);
    }

    /**
     * 待处理：工作人员即employee表中shop_id为空，即平台工作人员可以查看待处理内容
     * @param msgQuery
     * @param id
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery msgQuery, Long logininfoId) {
        // 根据loginminfoId查询额mployee表中的数据
        Employee employee = employeeMapper.findByLogininfoId(logininfoId);
        // 如果shop_id为空，将其存入查询条件中
        if (employee.getShop_id() == null){
            msgQuery.setShop_id(employee.getShop_id());
        }
        // 未处理
        msgQuery.setState(0);
        // 调用父类分页查询方法
        PageList<SearchMasterMsg> pageList = super.queryList(msgQuery);
        return pageList;
    }

    /**
     * 已处理：工作人员即employee表中shop_id不为空，即店铺工作人员可以查看已处理内容
     * @param msgQuery
     * @param id
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery msgQuery, Long logininfoId) {
        // 根据loginminfoId查询额mployee表中的数据
        Employee employee = employeeMapper.findByLogininfoId(logininfoId);
        // 如果shop_id不为空，将其存入查询条件中
        if (employee.getShop_id() != null){
            msgQuery.setShop_id(employee.getShop_id());
        }
        // 已处理
        msgQuery.setState(1);
        // 调用父类分页查询方法
        PageList<SearchMasterMsg> pageList = super.queryList(msgQuery);
        return pageList;
    }

    /**
     * 处理待处理的数据
     * @param pet
     */
    @Override
    @Transactional
    public void handle(Pet pet, Long logininfoId) {
        // 通过search_master_msg_id改变待处理的宠物的状态，即只要修改状态一定会改变
        searchMasterMsgMapper.updateBySSMId(pet.getSearch_master_msg_id());
        // 将修改后的宠物信息存入pet，pet_detail
        // 先添加pet表，获取自增长主键
        pet.setState(0); // 0下架，1上架卖出
        petMapper.add(pet);
        // 后添加pet_detail
        PetDetail petDetail = new PetDetail();
        petDetail.setIntro(pet.getDetail().getIntro());
        petDetail.setAdoptNotice(pet.getDetail().getAdoptNotice());
        petDetail.setPet_id(pet.getId());
        petDetailMapper.add(petDetail);

    }
}
